package tool

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

var RedisCon redis.Conn
var err error

func init() {
	RedisCon, err = redis.Dial("tcp", ":6379")
	if err != nil {
		fmt.Println(err)
		return
	}
}
func RedisSet(key string, value interface{}, expire int) bool {
	setError := RedisCon.Send("set", key, value)
	if setError != nil {
		fmt.Println("----设置redis:"+key+":错误----", setError)
		return false
	}
	if expire != 0 {
		expireError := RedisCon.Send("expire", key, expire)
		if expireError != nil {
			fmt.Println("----获取redis过期时间:"+key+":错误----", setError)
			return false
		}
	}
	fliError := RedisCon.Flush()
	if fliError != nil {
		fmt.Println("----redis:"+key+":运行错误----", setError)
		return false
	}
	return true
}

/*func RedisGet(key string)interface{}{
	getError := Redis.Send("get", key)
	if getError != nil{
		fmt.Println("----获取redis:"+key+":错误----",getError)
		return nil
	}
	value, ReceiveErr := Redis.Receive()
	if ReceiveErr != nil{
		fmt.Println("----获取redis:"+key+":错误----",ReceiveErr)
	}
	fmt.Println(value)
	return true
	//redis.ReceiveWithTimeout(Redis,2123123)
}*/
func RedisGet(key string) interface{} {
	value, ReceiveErr := redis.String(RedisCon.Do("get", key))
	if ReceiveErr != nil {
		fmt.Println("----获取redis-info:错误----", ReceiveErr)
	}
	return value
}
